package com.ifuntimes.http;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ifuntimes.exception.ServiceException;
import com.ifuntimes.utils.ConfigHelper;



public class ServerConfig {

	private static Logger logger = LoggerFactory.getLogger(ServerConfig.class);
	private static ConfigHelper configHelper;

	private static final String FILENAME = "conf/server.conf";

	private static final String SERVER_PORT = "server.port";
	private static final String SERVER_IP = "server.ip";
	private static final String ACK_TIMEOUT = "server.ack_timeout";
	private static final String APHB_TIMEOUT = "server.aphb_timeout";
	private static final String JERSEY_SERVER_PORT = "server.jersey.port";
	private static final String AIR_TIMEOUT = "server.air_timeout";
	private static final String TRY_TIMES = "server.try_times";
	private static final String ROAMING = "server.roaming";
	private static final String ESLHB_TIMEOUT = "server.eslhb_timeout";
	private static final String QUEUE_TIMEOUT = "server.queue_timeout";

	private static final String AUTO_BIND = "server.heartbeat.autobind";
	private static final String AUTO_REG = "server.heartbeat.auto_reg";

	private static final String TCP_SEND_HANDLE = "server.main.tcpHandler_sleep";
	private final static String READ_DB_SLEEP = "server.main.read_db_sleep";

	static {
		try {
			configHelper = new ConfigHelper(FILENAME);
		} catch (IOException e) {
			logger.error("Config file not found", e);
		}
	}

	public static String getIp() throws ServiceException {
		String ip;
		try {
			ip = configHelper.getString(SERVER_IP);
		} catch (Exception e) {
			logger.error("ip address error:", e);
			throw new ServiceException("ip address error: " + e);
		}
		return ip;
	}


	public static Integer getJerseyPort() throws ServiceException {
		Integer port;
		try {
			port = configHelper.getInteger(JERSEY_SERVER_PORT);
			if (port < 0 || port > 0xFFFF)
				throw new ServiceException( "Port value out of range: " + port);
			return port;
		} catch (Exception e) {
			logger.error("Server port read error", e);
			throw new ServiceException("Port value out of range: " + e);
		}
	}	   

}
